//
//  RoomTable_iPad.m
//  PokerPlus
//
//  Created by sunrui-MacBook on 11-8-9.
//  Copyright 2011 139.ME. All rights reserved.
//

#import "RoomTable_iPad.h"


@implementation RoomTable_iPad

@synthesize deskInfo,deskInfoLast;
int seatNum;
UIButton * tableBtn[9];
UIButton * btnViewBtn[6];
RoomTable_iPad * RoomTableSelf;

-(void)setDeskInfo:(UpdateDesk)ud{
	
	deskInfoLast = deskInfo;
	deskInfo = ud;
	NSLog(@"～～～～～～～～～桌子状态现在是:%d",deskInfo.state);
	[self UserOnTable];
    
	if (deskInfo.state < s_waitPreFlop) {
		return;
	}
	NSLog(@"开始游戏,Dealer在：%d，turn轮到谁了：%d",deskInfo.dealerSeat,deskInfo.turn);
	
	//显示盲注一次、
	NSLog(@"小盲注位置：%d，大盲注位置：%d",deskInfo.smallBSeat,deskInfo.bigBSeat);
    [self checkAbandonCard];
	[self showPoker];
	[self stopClock];
	[self animateClock:deskInfo.turn];
    [self btnViewBtnUnselect];
	[self upDataCardFirst:NO];
	
	if (deskInfo.state == s_waitGameStart) {
		//[self resetAll];
    
	}else if (deskInfo.state == s_waitPreFlop) {
	}else if (deskInfo.state == s_waitFlopRound) {
		
	}else if (deskInfo.state == s_waitReStart) {
		[self resetAll];
		[self removeCard];
	}
}

-(void)resetAll{
	//重置btnView;
	for (int i = 0; i < 9; i++) {
		if (tempImage1[i]) {
			//[tempImage1[i] removeFromSuperview];
			//[tempImage2[i] removeFromSuperview];
			[tempImage1[i] release];
			[tempImage2[i] release];
			tempImage1[i] = nil;
			tempImage2[i] = nil;
		}
		
	}
	_showPoker = NO;
	if (clock) {
		[clockImageView stopAnimating];
		[clockImageView setHidden:YES];
		[clock invalidate];
		clock = nil;
	}
	
	for (int i = 0 ;i < 6; i++) {
		[btnViewBtn[i] setSelected:NO];
		[btnViewBtn[i] setEnabled:NO];
	}

	//[table tableDisAppear];
	//加载用户信息:
	
	
}

-(void)showPoker{
	if (_showPoker) {
		for (int i = 0; i < 9; i++) {
			if (!deskInfo.isPlaying[i]) {
				//等待下一轮
				if (tempImage1[i]) {
					[tempImage1[i] removeFromSuperview];
					[tempImage2[i] removeFromSuperview];
					[tempImage1[i] release];
					[tempImage2[i] release];
					tempImage1[i] = nil;
					tempImage2[i] = nil;					
				}
			}
			else {
				
			}

		}
		return;
	}
	NSLog(@"showPoker");
	for (int i =0 ; i < 9; i++) {
		if (deskInfo.isPlaying[i]) {
			if (i == seatNum) {
				int poker1 = deskInfo.holeCards[0];
				int poker2 = deskInfo.holeCards[1];
				[self animatePoker:i :poker1 :poker2];
                
			}
			else {
				[self animatePoker:i :-1 :-1];
			}
			_showPoker = YES;
		}
	}
}

-(void)animatePoker:(int)seatNumber :(int)poker1 :(int)poker2{
	NSLog(@"poker1:%d,poker2:%d",poker1,poker2);
    int distance;
    if (poker1==-1) {
        tempImage1[seatNumber]=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"扑克背面.png"]] ;
        tempImage1[seatNumber].center=CGPointMake(self.frame.size.width/2, self.frame.size.height/2-30);
        tempImage1[seatNumber].transform=CGAffineTransformMakeScale(0.5, 0.5);
        tempImage2[seatNumber]=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"扑克背面.png"]];
        tempImage2[seatNumber].center=CGPointMake(self.frame.size.width/2, self.frame.size.height/2-30);
        tempImage2[seatNumber].transform=CGAffineTransformMakeScale(0.5, 0.5);
        distance = 10;

    }
    else{
        
        tempImage1[seatNumber]=[[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",poker1]]];
         [tempImage1[seatNumber] setFrame:CGRectMake(0, 0, 36, 54)];
		tempImage1[seatNumber].center=CGPointMake(self.frame.size.width/2, self.frame.size.height/2-30);
       
        
        tempImage2[seatNumber]=[[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",poker2]]] ;
        [tempImage2[seatNumber] setFrame:CGRectMake(0, 0, 36, 54)];
        tempImage2[seatNumber].center=CGPointMake(self.frame.size.width/2, self.frame.size.height/2-30);
        distance = 40;
    }
    [self addSubview:tempImage1[seatNumber]];
    [self addSubview:tempImage2[seatNumber]];
    tempImage1[seatNumber].alpha=0.0;
    tempImage2[seatNumber].alpha=0.0;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.6];
    
    
    switch (seatNumber) {
        case 0:
        {
            tempImage1[seatNumber].center=CGPointMake(340, 310);
            tempImage2[seatNumber].center=CGPointMake(340+distance, 310);
            
            break;
        }
        case 1:{
            tempImage1[seatNumber].center=CGPointMake(670, 310);
            tempImage2[seatNumber].center=CGPointMake(670+distance, 310);
          
            break;
        }
        case 2:{
            tempImage1[seatNumber].center=CGPointMake(740, 345);
            tempImage2[seatNumber].center=CGPointMake(740+distance, 345);
            
            break;
        }
        case 3:{
            tempImage1[seatNumber].center=CGPointMake(750, 440);
            tempImage2[seatNumber].center=CGPointMake(750+distance, 440);
            
            break;
        }
        case 4:{
            tempImage1[seatNumber].center=CGPointMake(655, 480);
            tempImage2[seatNumber].center=CGPointMake(655+distance, 480);
            
            break;
        }
        case 5:{
            tempImage1[seatNumber].center=CGPointMake(480, 480);
            tempImage2[seatNumber].center=CGPointMake(480+distance, 480);
            
            break;
        }
        case 6:{
            tempImage1[seatNumber].center=CGPointMake(325, 480);
            tempImage2[seatNumber].center=CGPointMake(325+distance, 480);
           
            break;
        }
        case 7:{
            tempImage1[seatNumber].center=CGPointMake(230, 440);
            tempImage2[seatNumber].center=CGPointMake(230+distance, 440);
            
            break;
        }
        case 8:{
            tempImage1[seatNumber].center=CGPointMake(240, 345);
            tempImage2[seatNumber].center=CGPointMake(240+distance, 345);
            
            break;
        }
        default:
            break;
    }
    tempImage1[seatNumber].alpha=1.0;
    tempImage2[seatNumber].alpha=1.0;
    [UIView commitAnimations];
}

-(void)animateClock:(int)seatNumber{
	if (!clock.isValid) {
		clocktime = 0;
		[clockImageView setCenter:tableBtn[seatNumber].center];
		clock = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timeisUP) userInfo:nil repeats:YES];
	}
}

-(void)stopClock{
	if (clock && clock.isValid && (deskInfo.turn != deskInfoLast.turn || deskInfo.state != deskInfoLast.state)) {
		[clockImageView stopAnimating];
		[clockImageView setHidden:YES];
		[clock invalidate];
		clock = nil;
	}
}

- (void)timeisUP{
	
	NSLog(@"%d",clocktime);
	
	if (clocktime == 0) {
		[clockImageView setHidden:NO];
		[clockImageView startAnimating];
		clocktime ++;
	}
	else if (clocktime < isFast * 1) {
		clocktime ++;
	}
	else {
		[clockImageView stopAnimating];
		[clockImageView setHidden:YES];
		[clock invalidate];
		clock = nil;
		if (deskInfo.turn == seatNum) {
			//[self StandUp];
			if (seatNum != -1) {
				NSLog(@"时间到，站起");
				[[AppDelegate sharedDelegate] cmdStandUp:socketLogin atIndex:seatNum];
			}
		}
	}
}

- (void)awakeFromNib{
	tableBtn[0] = tableBtn1;
	tableBtn[1] = tableBtn2;
	tableBtn[2] = tableBtn3;
	tableBtn[3] = tableBtn4;
	tableBtn[4] = tableBtn5;
	tableBtn[5] = tableBtn6;
	tableBtn[6] = tableBtn7;
	tableBtn[7] = tableBtn8;
	tableBtn[8] = tableBtn9;
	
	btnViewBtn[0] = btnViewBtn1;
	btnViewBtn[1] = btnViewBtn2;
	btnViewBtn[2] = btnViewBtn3;
	btnViewBtn[3] = btnViewBtn4;
	btnViewBtn[4] = btnViewBtn5;
	btnViewBtn[5] = btnViewBtn6;

	
	socketLogin = [[AppDelegate sharedDelegate] mainsocket];
	eventLogin = [[AppDelegate sharedDelegate] eventLogin];
	
	IMP OnSitDown = [self methodForSelector:@selector(OnSitDown:)];
	IMP OnStandUp = [self methodForSelector:@selector(OnStandUp:)];
	IMP OnSitDownFailure = [self methodForSelector:@selector(OnSitDownFailure:)];
	
	[eventLogin AddCmd:hchSitDown fun:(CMDFUN)OnSitDown];
	[eventLogin AddCmd:hhcSitDownSuccess fun:(CMDFUN)OnSitDown];
	[eventLogin AddCmd:hhcSitDownFailure fun:(CMDFUN)OnSitDownFailure];
	
	[eventLogin AddCmd:hchStandUp fun:(CMDFUN)OnStandUp];
	[eventLogin AddCmd:hhcStandUpSuccess fun:(CMDFUN)OnStandUp];
	seatNum = -1;
	
	deskInfo = NewUpdateNon();
	deskInfoLast= NewUpdateNon();
	
	NSMutableArray * clockArray = [NSMutableArray array];
	
	for (int i = 1 ; i <= 73; i++) {
		//NSLog(@"%@",[NSString stringWithFormat:@"clock-00%02d.png",i]);
		[clockArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"clock-00%02d.png",i]]];
	}
	isFast = 20;
	clockImageView.animationImages = clockArray;
	clockImageView.animationDuration = isFast;
	clockImageView.animationRepeatCount = 2;
	
	RoomTableSelf = self;
	
	//[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(StandUp) name:@"EnterBackground" object:nil];
    [self addAllToPrepare];
}



- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder{
	self = [super initWithCoder:aDecoder];
	if (self) {

	}
	return self;
}

- (void)tableAppear{
//	for (int i =0; i < 9; i++) {
//		tableBtn[i].selected = NO;
//		tableDelegate.deskInfo.rui[i].usrId = -1;
//		tableDelegate.deskInfoLast.rui[i].usrId = -1;
//	}
//	seatNum = -1;
		[self performSelector:@selector(upDataCardFirst:) withObject:[NSNumber numberWithBool:YES] afterDelay:1];
}

- (void)tableDisAppear{

	deskInfo = NewUpdateNon();
	deskInfoLast= NewUpdateNon();
	NSLog(@"table DisAppear");
	for (int i =0; i < 9; i++) {
		tableBtn[i].selected = NO;		
		//NSLog(@"change:%d",tableDelegate.deskInfoLast.rui[i].usrId);
	}
	//seatNum = -1;
	[self StandUp];
    [self removeCard];
}

-(IBAction)buttonPressed:(UIButton *)sender{

	if (sender.selected || seatNum != -1) {
		//请求信息
		NSLog(@"%d",sender.tag);
		return;
	}
	NSLog(@"ready to sit down");
	
	[[AppDelegate sharedDelegate] cmdSitDown:socketLogin atIndex:sender.tag];
	NSLog(@"%d",sender.tag);
	seatNum = sender.tag;
}

- (IBAction)btnViewButtonPressed:(UIButton *)sender{
	int command,autocommand;
	long long Chips = 0;
	switch (sender.tag) {
		case 0:
			command = act_fold;
			autocommand = auto_foldany;
			break;
		case 1:
			command = act_check;
			autocommand = auto_check;
			break;
		case 2:
			command = act_call;
			autocommand = auto_callnow;
			break;
		case 3:
			command = act_raise;
			autocommand = auto_wait;
			Chips = 2;//设置跟注多少
			break;
		case 4:
			command = act_null;
			autocommand = auto_checkfold;
			break;
		case 5:
			command = act_null;
			autocommand = auto_callany;
			break;
		default:
			break;
	}
	if (sender.selected) {
		sender.selected = NO;
		[self btnViewBtnExecute:hchSetAutoAction withComand:auto_wait andChipsData:Chips];
		return;
	}
	if (deskInfo.turn == seatNum && command != act_null) {
		//动作
		[self btnViewBtnExecute:hchCommand withComand:command andChipsData:Chips];
		for (int i = 0; i < 6;  i++) {
			btnViewBtn[i].enabled = NO;
		}
	}
	else {

		//自动动作
		NSLog(@"自动动作");
		[self btnViewBtnExecute:hchSetAutoAction withComand:autocommand andChipsData:Chips];
		sender.selected = YES;
	}
}

-(void)btnViewBtnExecute:(int)Message withComand:(int)Command andChipsData:(long long)data{
	[socketLogin NewMessage:Message];
	ClientCommand cc = NewClientCommand(Command, data);
	NSData * ccdat = [NSData dataWithBytes:&cc length:sizeof(ClientCommand)];
	[socketLogin WriteData:ccdat];
	[socketLogin Send];
}

-(void)btnViewBtnUnselect{
	if (seatNum == -1) {
		return;
	}

	if (deskInfo.state != deskInfoLast.state || deskInfo.turn != deskInfoLast.turn) {
		for (int i =0 ; i < 6; i++) {
			btnViewBtn[i].selected = NO;
		}
		if (deskInfo.turn == seatNum) {
			NSLog(@"轮到按了");
			btnViewBtn[0].enabled = YES;
			btnViewBtn[1].enabled = YES;
			btnViewBtn[2].enabled = YES;
			btnViewBtn[3].enabled = YES;
			[btnViewBtn[0] setImage:[UIImage imageNamed:@"盖牌无.png"] forState:UIControlStateNormal];
			[btnViewBtn[1] setImage:[UIImage imageNamed:@"过牌无.png"] forState:UIControlStateNormal];
			[btnViewBtn[2] setImage:[UIImage imageNamed:@"跟注无.png"] forState:UIControlStateNormal];

			btnViewBtn[4].enabled = NO;
			btnViewBtn[5].enabled = NO;
		}
		else {
			NSLog(@"按auto命令");
			[btnViewBtn[0] setImage:[UIImage imageNamed:@"盖牌方框.png"] forState:UIControlStateNormal];
			[btnViewBtn[1] setImage:[UIImage imageNamed:@"过牌方框.png"] forState:UIControlStateNormal];
			[btnViewBtn[2] setImage:[UIImage imageNamed:@"跟注方框.png"] forState:UIControlStateNormal];
			btnViewBtn[0].enabled = YES;
			btnViewBtn[1].enabled = YES;
			btnViewBtn[2].enabled = NO;
			btnViewBtn[3].enabled = NO;
			
			btnViewBtn[4].enabled = YES;
			btnViewBtn[5].enabled = YES;
		}

	}
	NSLog(@"玩家当前轮所下的筹码数%lld,当前轮最大的注%lld",deskInfo.stake[seatNum],deskInfo.lastMaxMoney);
	if (deskInfo.stake[seatNum] < deskInfo.lastMaxMoney) {
		btnViewBtn[1].enabled = NO;//不能过牌
	}
	
	if (!deskInfo.lastMaxMoney || deskInfo.stake[seatNum] && deskInfo.stake[seatNum] >= deskInfo.lastMaxMoney) {
		btnViewBtn[2].enabled = NO;
	}
	
	if (deskInfo.money[seatNum] < deskInfo.lastMaxMoney - deskInfo.stake[seatNum]) {
		NSLog(@"只能all in");
	}
	
}

-(IBAction)StandUp{
	NSLog(@"起立前:%d",seatNum);
	if (seatNum != -1) {
		[[AppDelegate sharedDelegate] cmdStandUp:socketLogin atIndex:seatNum];
	}
}


- (void)dealloc {
    [super dealloc];
}

- (void)UserOnTable{
	NSLog(@"桌面人员更新");
	for (int i =0 ; i < 9; i++) {
		NSLog(@"几个人:%d,id:%d,LastId:%d",i,deskInfo.rui[i].usrId,deskInfoLast.rui[i].usrId);
		if (deskInfo.rui[i].usrId != deskInfoLast.rui[i].usrId) {
			if (deskInfo.rui[i].usrId != -1) {
				
				int num = deskInfo.rui[i].position;
				NSLog(@"position:%d",num);
				[UIView beginAnimations:nil context:nil];
				//[UIView setAnimationDelay:1.2f];
				[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
				[UIView setAnimationDuration:0.5f];
				[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:tableBtn[num] cache:YES];
				tableBtn[num].hidden = NO;
				tableBtn[num].selected = YES;
				[UIView commitAnimations];
			}
			else {
				int num = deskInfo.rui[i].position;
				NSLog(@"position:%d",num);
				[UIView beginAnimations:nil context:nil];
				//[UIView setAnimationDelay:1.2f];
				[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
				[UIView setAnimationDuration:0.5f];
				[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:tableBtn[num] cache:YES];
				tableBtn[num].selected = NO;
				if (seatNum != -1) {
					tableBtn[num].hidden = YES;
				}				
				[UIView commitAnimations];
				if (num == seatNum) {
					seatNum = -1;
				}
			}
		}
			
	}
	
}

#pragma mark -

-(void)OnSitDown:(SocketEngine *)socket{
	NSLog(@"sit down ");
//	[UIView beginAnimations:nil context:nil];
//	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//	[UIView setAnimationDuration:0.5f];
//	[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:tableBtn[seatNum] cache:YES];
//	tableBtn[seatNum].selected = YES;
//	[UIView commitAnimations];
//	NSLog(@"%x",socket);
	//[tableDelegate setDeskInfo:NewUpdate([socket ReadData:(sizeof(UpdateDesk))])];
	
	for (int i = 0 ; i < 9; i++) {
		if (i != seatNum && !tableBtn[i].selected) {
			[tableBtn[i] setHidden:YES];
		}
	}
//	for (int i = 0; i < 3; i++) {
//		[btnViewBtn[i] setEnabled:YES];
//	}
//	btnViewBtn[4].enabled = NO;
//	btnViewBtn[5].enabled = NO;
}

-(void)OnSitDownFailure:(SocketEngine *)socket{
	seatNum = -1;
}

-(void)OnStandUp:(SocketEngine *)socket{
	NSLog(@"stand up.. %d",seatNum);
//	[UIView beginAnimations:nil context:nil];
//	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//	//[UIView setAnimationDuration:0.5f];
//	[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:tableBtn[seatNum] cache:YES];
//	tableBtn[seatNum].selected = NO;
//	[UIView commitAnimations];
	seatNum = -1;
	[RoomTableSelf stopClock];
	for (int i = 0; i < 9; i++) {
		[tableBtn[i] setHidden:NO];
	}
	for (int i = 0 ;i < 6; i++) {
		[btnViewBtn[i] setEnabled:NO];
	}
}

//以下检测+盖牌动画
-(void)checkAbandonCard{
    
    for (int m=0; m<9; m++) {
        if(deskInfo.isPlaying[m]!=deskInfoLast.isPlaying[m]&&!deskInfo.isPlaying[m])
        {
            float width1=(tempImage1[m].center.x-self.frame.size.width/2)/4;
            float height1=(tempImage1[m].center.y-self.frame.size.height/2)/3;
            float width2=(tempImage2[m].center.x-self.frame.size.width/2)/4;
            float height2=(tempImage2[m].center.y-self.frame.size.height/2)/3;
            
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.2];
            tempImage1[m].center=CGPointMake(tempImage1[m].center.x-width1, tempImage1[m].center.y-height1);
            tempImage2[m].center=CGPointMake(tempImage2[m].center.x-width2, tempImage2[m].center.y-height2);
            tempImage1[m].alpha=0.0;
            tempImage2[m].alpha=0.0;
            tempImage1[m].transform=CGAffineTransformMakeScale(0.1, 0.1);
            tempImage2[m].transform=CGAffineTransformMakeScale(0.1, 0.1);
            [UIView commitAnimations];
        }
    }
    
}



//以下公共牌操作
-(void)addAllToPrepare{
    if (p1==nil) {
        p1=[[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width/2-105, 365, 36, 54)];
    }
    for (UIView *v in [p1 subviews]) {
        [v removeFromSuperview];
    }
    [self addSubview:p1];
    
    if (p2==nil) {
        p2=[[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width/2-65, 365, 36, 54)];
    }
    for (UIView *v in [p2 subviews]) {
        [v removeFromSuperview];
    }
    [self addSubview:p2];
    
    if (p3==nil) {
        p3=[[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width/2-25, 365, 36, 54)];
    }
    for (UIView *v in [p3 subviews]) {
        [v removeFromSuperview];
    }
    [self addSubview:p3];
    if (p4==nil) {
        p4=[[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width/2+15, 365, 36, 54)];
    }
    for (UIView *v in [p4 subviews]) {
        [v removeFromSuperview];
    }
    [self addSubview:p4];
    if (p5==nil) {
        p5=[[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width/2+55, 365, 36, 54)];
    }
    for (UIView *v in [p5 subviews]) {
        [v removeFromSuperview];
    }
    [self addSubview:p5];
    
    
}

-(void)upDataCardFirst:(BOOL)b{
	if (deskInfo.state < s_waitFlopRound || deskInfo.state >s_gameEnd) {
		return;
	}

    NSLog(@"Num::%d",deskInfo.boardCardsNum);
    if (deskInfo.boardCardsNum-deskInfoLast.boardCardsNum>0) {
        //        NSLog(@"0:::%d",deskInfo.boardCards[0]);
        //        NSLog(@"1:::%d",deskInfo.boardCards[1]);
        //        NSLog(@"2:::%d",deskInfo.boardCards[2]);
        //        NSLog(@"3:::%d",deskInfo.boardCards[3]);
        //        NSLog(@"4:::%d",deskInfo.boardCards[4]);
        
        switch (deskInfo.boardCardsNum) {
            case 3:
            {
                
                [self addP3];
                break;
            }
            case 4:{
                if (b) {
                    [self addP3];
                    [self addP4WithDelay:YES];
                }
                else
                    [self addP4WithDelay:NO];
                break;
            }
            case 5:{
                if (b) {
                    [self addP3];
                    [self addP4WithDelay:YES];
                    [self addP5WithDelay:YES];
                }
                else
                    [self addP5WithDelay:NO];
                break;
            }
            default:{
                
                [self removeCard];
                break;
            }
        }
    }
    else{
        //[self removeCard];
    }
    
}

-(void)addP3{
    //翻牌效果
    
    UIImageView *q1=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 36, 54)];
    q1.image=[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",deskInfo.boardCards[0]]];
    q1.tag=1;
    
    UIImageView *q2=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 36, 54)];
    q2.image=[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",deskInfo.boardCards[1]]];
    q2.tag=2;
    
    UIImageView *q3=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 36, 54)];
    q3.image=[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",deskInfo.boardCards[2]]];
    q3.tag=3;
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:p1 cache:NO];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelay:0.0];
    [p1 addSubview:q1];
    [UIView commitAnimations];
    [q1 release];
    
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:p2 cache:NO];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelay:0.3];
    [p2 addSubview:q2];
    [UIView commitAnimations];
    [q2 release];
    
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:p3 cache:NO];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelay:0.6];
    [p3 addSubview:q3];
    [UIView commitAnimations];
    [q3 release];
    
}
-(void)addP4WithDelay:(BOOL)b{
    float d=0.0;
    if (b) {
        d=0.9;
    }
    UIImageView *q4=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 36, 54)];
    q4.image=[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",deskInfo.boardCards[3]]];
    q4.tag=4;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:p4 cache:NO];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelay:d];
    [p4 addSubview:q4];
    [UIView commitAnimations];
    [q4 release];
    
}
-(void)addP5WithDelay:(BOOL)b{
    float d=0.0;
    if (b) {
        d=1.2;
    }
    
    UIImageView *q5=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 36, 54)];
    q5.image=[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",deskInfo.boardCards[4]]];
    q5.tag=5;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:p5 cache:NO];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelay:d];
    [p5 addSubview:q5];
    [UIView commitAnimations];
    [q5 release];
}

//扣牌动画
-(void)removeCard{
    
    NSLog(@"移除桌牌");
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:p1 cache:NO];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDelay:0];
    [[p1 viewWithTag:1] removeFromSuperview];    
    [UIView commitAnimations];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:p2 cache:NO];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDelay:0.2];
    [[p2 viewWithTag:2] removeFromSuperview];
    [UIView commitAnimations];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:p3 cache:NO];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDelay:0.4];
    [[p3 viewWithTag:3] removeFromSuperview];
    [UIView commitAnimations];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:p4 cache:NO];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDelay:0.6];
    [[p4 viewWithTag:4] removeFromSuperview];
    [UIView commitAnimations];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:p5 cache:NO];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDelay:0.8];
    [[p5 viewWithTag:5] removeFromSuperview];
    [UIView commitAnimations];
    
    
}
@end
